﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Lohnefink3D
{
    /// <summary>
    /// Class that creates an object to draw the 3D axes on the screen
    /// </summary>
    class My3DObject
    {
        #region Declaration
        
        private GraphicsDevice device;

        /// <summary>
        /// The set of Vertex that will be drawn into the screen
        /// </summary>
        private VertexBuffer vertexBuffer;

        /// <summary>
        /// The Effect that will be used to draw the 3D figure
        /// </summary>
        private BasicEffect effect;

        /// <summary>
        /// The matrix that represents the transformations of the real world
        /// </summary>
        public Matrix worldMatrix = Matrix.Identity;

        //public static KeyboardState keyboard;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="graphicsDevice">O dispositivo gráfico</param>
        public My3DObject(GraphicsDevice graphicsDevice, float axisLength)
        {
            device = graphicsDevice;

            effect = new BasicEffect(device);

            //float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;

            effect.View =
            Matrix.CreateLookAt(//cria a camera
            new Vector3(2.0f, 2.0f, 2.0f),//nesta posicao
            Vector3.Zero,//olhando pra ca
            Vector3.Up);//virada pra "cima"

            effect.Projection =
            Matrix.CreatePerspectiveFieldOfView(//"tela"
            MathHelper.ToRadians(45.0f),//abertura
            device.Viewport.AspectRatio,//proporção
            1.0f,//"near"
            10.0f);//"far"

            //effect.LightingEnabled = false;
            effect.VertexColorEnabled = true;

            Create3DAxis(axisLength);
        }
        #endregion

        #region Unload Content
        public void UnloadContent()
        {

        }
        #endregion

        #region My Code
        /// <summary>
        /// 3D Axes Drawing Definition
        /// </summary>
        private void Create3DAxis(float axisLength)
        {
            int vertexCount = 6;

            VertexPositionColor[] vertices = new VertexPositionColor[vertexCount];

            vertices[0] = new VertexPositionColor(new Vector3(-axisLength, 0.0f, 0.0f), new Color(0, 0, 0));
            vertices[1] = new VertexPositionColor(new Vector3(axisLength, 0.0f, 0.0f), new Color(255, 0, 0));

            vertices[2] = new VertexPositionColor(new Vector3(0.0f, -axisLength, 0.0f), new Color(0, 0, 0));
            vertices[3] = new VertexPositionColor(new Vector3(0.0f, axisLength, 0.0f), new Color(0, 255, 0));

            vertices[4] = new VertexPositionColor(new Vector3(0.0f, 0.0f, -axisLength), new Color(0, 0, 0));
            vertices[5] = new VertexPositionColor(new Vector3(0.0f, 0.0f, axisLength), new Color(0, 0, 255));

            vertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertexCount, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColor>(vertices);
        }
        #endregion

        #region Draw
        /// <summary>
        /// 3D Axes Drawing
        /// </summary>
        public void Draw()
        {
            effect.World = worldMatrix;

            device.SetVertexBuffer(vertexBuffer);

            foreach (EffectPass CurrentPass in effect.CurrentTechnique.Passes)
            {
                CurrentPass.Apply();
                device.DrawPrimitives(PrimitiveType.LineList, 0, 3);
            }
        }
        #endregion
    }
}
